package com.ruoyi.machine.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.machine.EmpMachineType;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.machine.service.IEmpMachineTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 机台类型Controller
 *
 * @author 黄字添
 * @date 2024-01-09
 */
@RestController
@RequestMapping("/machine/machineType")
public class EmpMachineTypeController extends BaseController {
    @Autowired
    private IEmpMachineTypeService empMachineTypeService;

    /**
     * 查询机台类型列表
     */
    @PreAuthorize("@ss.hasPermi('machine:machineType:list')")
    @GetMapping("/list")
    public TableDataInfo list(EmpMachineType empMachineType) {
        startPage();
        List<EmpMachineType> list = empMachineTypeService.selectEmpMachineTypeList(empMachineType);
        
        return getDataTable(list);
    }
    
    /**
     * 获取机台类型字典
     */
    @GetMapping("/machineTypeDict")
    public AjaxResult machineTypeDict() {
        return success(empMachineTypeService.selectEmpMachineTypeDict());
    }
    /**
     * 获取门店相关的机型类型，和设置的排序
     */
    @PreAuthorize("@ss.hasPermi('machine:machineType:list')")
    @GetMapping("/getMachineTypeSortList")
    public TableDataInfo getMachineTypeSortList(EmpMachineType empMachineType) {
        startPage();
        List<EmpMachineType> list = empMachineTypeService.selectEmpMachineTypeSortList(empMachineType);
        return getDataTable(list);
    }
    

    /**
     * 导出机台类型列表
     */
    @PreAuthorize("@ss.hasPermi('machine:machineType:export')")
    @Log(title = "机台类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EmpMachineType empMachineType) {
        List<EmpMachineType> list = empMachineTypeService.selectEmpMachineTypeList(empMachineType);
        ExcelUtil<EmpMachineType> util = new ExcelUtil<EmpMachineType>(EmpMachineType.class);
        util.exportExcel(response, list, "机台类型数据");
    }

    /**
     * 获取机台类型详细信息
     */
    @PreAuthorize("@ss.hasPermi('machine:machineType:query')")
    @GetMapping(value = "/{ID}")
    public AjaxResult getInfo(@PathVariable("ID") Long ID) {
        return success(empMachineTypeService.selectEmpMachineTypeByID(ID));
    }

    /**
     * 新增机台类型
     */
    @PreAuthorize("@ss.hasPermi('machine:machineType:add')")
    @Log(title = "机台类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EmpMachineType empMachineType) {
        if (empMachineTypeService.selectEmpMachineTypeByType(empMachineType.getMachineType()) != null) {
            return AjaxResult.error("该类型已存在，添加失败");
        } else {
            empMachineType.setCreatedBy(getUsername());
            empMachineType.setCreatedTime(new Date());
            return toAjax(empMachineTypeService.insertEmpMachineType(empMachineType));
        }
    }

    /**
     * 修改机台类型
     */
    @PreAuthorize("@ss.hasPermi('machine:machineType:edit')")
    @Log(title = "机台类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EmpMachineType empMachineType) {
        empMachineType.setUpdatedBy(getUsername());
        empMachineType.setUpdatedTime(new Date());
        return toAjax(empMachineTypeService.updateEmpMachineType(empMachineType));
    }

    /**
     * 删除机台类型
     */
    @PreAuthorize("@ss.hasPermi('machine:machineType:remove')")
    @Log(title = "机台类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{IDs}")
    public AjaxResult remove(@PathVariable Long[] IDs) {
        return toAjax(empMachineTypeService.deleteEmpMachineTypeByIDs(IDs));
    }
}
